package liumengbaio.lolzhangyoubao.Activity.Adapter;

import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import java.net.URI;
import java.net.URL;

import liumengbaio.lolzhangyoubao.Activity.InFo.MoreVoiceInfo;
import liumengbaio.lolzhangyoubao.R;

/**
 * Created by lanouhn on 16/9/14.
 */
public class MoreVoiceAdapter extends RecyclerView.Adapter {

    private Context mContext;
    private MoreVoiceInfo info;

    public MoreVoiceAdapter(Context mContext, MoreVoiceInfo info) {
        this.mContext = mContext;
        this.info = info;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_more_voice, null);
        holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {

        final ViewHolder holder1 = (ViewHolder) holder;
        holder1.name.setText(info.getData().get(position).getName());

        String voice_url = info.getData().get(position).getPath_url();
        Uri uri = Uri.parse(voice_url);
        final MediaPlayer player = MediaPlayer.create(mContext, uri);

        holder1.play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                player.start();
                holder1.music.setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    public int getItemCount() {
        return info.getData().size();
    }

    private class ViewHolder extends RecyclerView.ViewHolder {

        private TextView name;
        private ImageView play, music;

        public ViewHolder(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.more_voice_name);
            play = (ImageView) itemView.findViewById(R.id.more_voice_play);
            music = (ImageView) itemView.findViewById(R.id.more_voice_music);
        }
    }
}
